package com.adtech.adms.system.web.action;

import java.util.List;

import com.adtech.adms.SessionKey;
import com.adtech.adms.commons.web.action.DataGridAction;
import com.adtech.adms.system.SystemConstants;
import com.adtech.adms.system.pojo.SysAdminLog;
import com.adtech.adms.system.pojo.User;
import com.adtech.adms.system.service.SystemLogService;
import com.adtech.core.dao.query.FilterItem;
import com.adtech.core.dao.query.FilterMap;
import com.adtech.core.dao.query.Pager;
import com.adtech.core.dao.query.Sort;
import com.adtech.core.dao.query.FilterItem.Formatters;
import com.adtech.core.dao.query.FilterItem.QueryType;
import com.adtech.core.exception.ServiceException;
import com.adtech.core.exception.SystemException;
import com.adtech.core.web.action.AjaxResult;

public class SystemLogAction extends DataGridAction{
	private static final long serialVersionUID = 4775177870632543937L;
	private SysAdminLog systemLog;
	private String id;
	private SystemLogService systemLogService;
	
	
	public SysAdminLog getSystemLog() {
		return systemLog;
	}

	public void setSystemLog(SysAdminLog systemLog) {
		this.systemLog = systemLog;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public SystemLogService getSystemLogService() {
		return systemLogService;
	}

	public void setSystemLogService(SystemLogService systemLogService) {
		this.systemLogService = systemLogService;
	}

	public String main(){
//		User loginUser = (User) session.get(SessionKey.SESSION_KEY_USER);
//		loginUserId = loginUser.getUserId();
		return SUCCESS;
	}

	@Override
	protected List<?> getModelDataGridList(Pager pager, FilterMap filtermap,
			Sort sort) throws SystemException, ServiceException {
		addUserCompanyCondition(filtermap);
		return systemLogService.getLogList(pager, filtermap.toExpression(), sort);
	}

	private void addUserCompanyCondition(FilterMap filtermap) {
//		User user = (User) session.get(SessionKey.SESSION_KEY_USER);
		FilterItem fullPathIdsItem = new FilterItem();
		fullPathIdsItem.setExpType(QueryType.LIKE.name());
		fullPathIdsItem.setFilterName(SystemConstants.User.FULLPATHIDS);
		fullPathIdsItem.setFormatter(Formatters.LikeFormatter.name());

		FilterItem idItem = new FilterItem();
		idItem.setExpType(QueryType.EQ.name());
		idItem.setFilterName(SystemConstants.User.COMPANY_ID);
//		idItem.setFilterValue(Long.toString(user.getDepartment().getCompany().getId()));

		String linkOperate  = "("+SystemConstants.User.FULLPATHIDS+"|"+SystemConstants.User.COMPANY+")";

		for(FilterItem item:filtermap.values()){
			if(item.getFilterValue()!=null){
				String value = item.getFilterValue().toString();
				if(value!=null && value.length()>0){
					linkOperate = item.getFilterName() + "&" + linkOperate;
				}
			}
		}

		filtermap.put(SystemConstants.User.FULLPATHIDS, fullPathIdsItem);
		filtermap.put(SystemConstants.User.COMPANY, idItem);
		filtermap.setLinkOperate(linkOperate);
	}
	
	public String delete() {
		if (id != null && id.length() > 0) {
			try {
				User currentUser = (User) session.get(SessionKey.SESSION_KEY_USER);
				systemLogService.delete(id,currentUser);
				setAjaxResult(AjaxResult.success());
			} catch (ServiceException e) {
//				log.error(e);
				setAjaxResult(AjaxResult.failed(e.getErrorCode(), e.getErrorMessage()));
			}
		}
		return SUCCESS;
	}

}
